package cn.stylefeng.guns.modular.carIndustryPlan.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.guns.modular.carIndustryPlan.entity.CarIndustryPlan;
import cn.stylefeng.guns.modular.carIndustryPlan.enums.CarIndustryPlanExceptionEnum;
import cn.stylefeng.guns.modular.carIndustryPlan.mapper.CarIndustryPlanMapper;
import cn.stylefeng.guns.modular.carIndustryPlan.pojo.request.CarIndustryPlanRequest;
import cn.stylefeng.guns.modular.carIndustryPlan.service.CarIndustryPlanService;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 汽车行业解决方案业务实现层
 *
 * @author liuxue
 * @date 2022/01/10 15:06
 */
@Service
public class CarIndustryPlanServiceImpl extends ServiceImpl<CarIndustryPlanMapper, CarIndustryPlan> implements CarIndustryPlanService {

	@Override
    public void add(CarIndustryPlanRequest carIndustryPlanRequest) {
        CarIndustryPlan carIndustryPlan = new CarIndustryPlan();
        BeanUtil.copyProperties(carIndustryPlanRequest, carIndustryPlan);
        this.save(carIndustryPlan);
    }

    @Override
    public void del(CarIndustryPlanRequest carIndustryPlanRequest) {
        CarIndustryPlan carIndustryPlan = this.queryCarIndustryPlan(carIndustryPlanRequest);
        this.removeById(carIndustryPlan.getCarIndustryPlanId());
    }

    @Override
    public void edit(CarIndustryPlanRequest carIndustryPlanRequest) {
        CarIndustryPlan carIndustryPlan = this.queryCarIndustryPlan(carIndustryPlanRequest);
        BeanUtil.copyProperties(carIndustryPlanRequest, carIndustryPlan);
        this.updateById(carIndustryPlan);
    }

    @Override
    public CarIndustryPlan detail(CarIndustryPlanRequest carIndustryPlanRequest) {
        return this.queryCarIndustryPlan(carIndustryPlanRequest);
    }

    @Override
    public PageResult<CarIndustryPlan> findPage(CarIndustryPlanRequest carIndustryPlanRequest) {
        LambdaQueryWrapper<CarIndustryPlan> wrapper = createWrapper(carIndustryPlanRequest);
        wrapper.orderByAsc(CarIndustryPlan::getSort);
        Page<CarIndustryPlan> sysRolePage = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(sysRolePage);
    }

    @Override
    public List<CarIndustryPlan> findList(CarIndustryPlanRequest carIndustryPlanRequest) {
        LambdaQueryWrapper<CarIndustryPlan> wrapper = this.createWrapper(carIndustryPlanRequest);
        return this.list(wrapper);
    }

    /**
     * 获取信息
     *
     * @author liuxue
     * @date 2022/01/10 15:06
     */
    private CarIndustryPlan queryCarIndustryPlan(CarIndustryPlanRequest carIndustryPlanRequest) {
        CarIndustryPlan carIndustryPlan = this.getById(carIndustryPlanRequest.getCarIndustryPlanId());
        if (ObjectUtil.isEmpty(carIndustryPlan)) {
            throw new ServiceException(CarIndustryPlanExceptionEnum.CARINDUSTRYPLAN_NOT_EXISTED);
        }
        return carIndustryPlan;
    }

    /**
     * 创建查询wrapper
     *
     * @author liuxue
     * @date 2022/01/10 15:06
     */
    private LambdaQueryWrapper<CarIndustryPlan> createWrapper(CarIndustryPlanRequest carIndustryPlanRequest) {
        LambdaQueryWrapper<CarIndustryPlan> queryWrapper = new LambdaQueryWrapper<>();

        Integer carIndustryPlanId = carIndustryPlanRequest.getCarIndustryPlanId();
        Long fileId = carIndustryPlanRequest.getFileId();
        String title = carIndustryPlanRequest.getTitle();
        String details = carIndustryPlanRequest.getDetails();
        String Introduction = carIndustryPlanRequest.getIntroduction();
        Integer sort = carIndustryPlanRequest.getSort();
        Integer status = carIndustryPlanRequest.getStatus();

        queryWrapper.eq(ObjectUtil.isNotNull(carIndustryPlanId), CarIndustryPlan::getCarIndustryPlanId, carIndustryPlanId);
        queryWrapper.eq(ObjectUtil.isNotNull(fileId), CarIndustryPlan::getFileId, fileId);
        queryWrapper.like(ObjectUtil.isNotEmpty(title), CarIndustryPlan::getTitle, title);
        queryWrapper.like(ObjectUtil.isNotEmpty(details), CarIndustryPlan::getDetails, details);
        queryWrapper.like(ObjectUtil.isNotEmpty(Introduction), CarIndustryPlan::getIntroduction, Introduction);
        queryWrapper.eq(ObjectUtil.isNotNull(sort), CarIndustryPlan::getSort, sort);
        queryWrapper.eq(ObjectUtil.isNotNull(status), CarIndustryPlan::getStatus, status);

        return queryWrapper;
    }

}